<?php
namespace app\user\controller;
use \think\Controller;
use \think\Request;
use \think\Session;
use app\user\traits\models\UserInfoCheck;
use app\user\traits\controllers\Other;
/**
 * 前台会员注册登录
 */
class User extends Controller
{  
    use UserInfoCheck;//use other;
    use \app\common\traits\controller\TraitController;
    ///////////////////////////////////注册/////////////////////////////////
    //前置操作
    protected $beforeActionList = [
        'checkLogin'=> ['except' => 'getlogin,postloginsuccess,getloginout,postsaveuser,postsendcode,getcheckpassword,getcheckcode,getcheckphone,getregister'],
    ];
    /**
     * [checkLogin 判断是否登陆]
     * @return [type] [description]
     */
    protected function checkLogin()
    {
        if (!model('Users')->checkLogin()) {
            Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect('@user/login');
            exit;
        }
    }
    /**
     * [getRegister 注册界面]
     * @return [type] [description]
     */
    public function getRegister()
    {
        $b = $this->old();
        if (empty($b)) {
            $b['phone']='';
            $b['imgCode']='';
            $b['phone_code']='';
        }
        return view('user/register', ['b' => $b]);
    }
    /**
     * [getCheckPhone 检测手机是否注册]
     * @return [type] [description]
     */
    public function getCheckPhone()
    {
        $phone = input('phone');
        $re = model('Users')->getUserInfo(['mobile'=>$phone]);
        if (empty($re)) {
            session('phone',$phone);
            return json(0);
        }
        return 1;
    } 
    /**
     * [getCheckCode 检测图片验证码]
     * @return [type] [description]
     */
    public function getCheckCode()
    {
        $code=input('imgCode');
        $captcha = new \think\captcha\Captcha(config('captcha'));
        if (!$captcha->check($code)) {
            return json(0);
        }
        return $captcha;
    }
    /**
     * [getCheckPassword 检验密码复杂度]
     * @return [type] [description]
     */
    public function getCheckPassword()
    {
        $password = input('password');
        $re = $this->cheack(['password'=>$password]);
        if ($re) {
            return 1;
        }else{
            return json(0);
        }
    }
    /**
     * [postSendCode 发送手机验证码]
     * @return [type] [description]
     */
    public function postSendCode()
    {
        $phone = session('phone');
        $re = model('PhoneCode')->send($phone);
        return $re;
    }
    /**
     * [postSaveUser 注册]
     * @return [type] [description]
     */
    public function postSaveUser()
    {
        $this->flash();
        $post_user_info['mobile'] = input('post.phone');
        $imgCode = input('post.imgCode');
        $phone_code = input('post.phone_code');
        $post_user_info['password'] = input('post.password');
        $repassword = input('post.repassword');
        $accepted = input('post.accepted');
        $re = model('PhoneCode')->check($phone_code,session_id());
        if (!$re) {
            return $this->error(model('PhoneCode')->getError());
        }
        $captcha = new \think\captcha\Captcha(config('captcha'));
        if (!$captcha->check($imgCode)) {
            return $this->error(lang('图片验证码错误'));
        }
        //修改cheack
        if (empty($accepted)) {
            return $this->error(lang('请同意双辉旅程网服务协议'),$url = null,'',$wait=3);
        }
        $res = $this->cheack(['password'=>input('post.password'),'repassword'=>$repassword]);
        if ($res) {
            //user入库
            $user=model('Users')->addUser($post_user_info);
            if ($user) {
                session('phone', null);
                //注册完直接登录
                $id = model('Users')->where('mobile',$post_user_info['mobile'])->value('id');
                $data['id'] = $id;
                $data['mobile'] = $post_user_info['mobile'];
                Session::set('user',$data);
                return $this->success(lang('注册成功'),'/userinfo/personinfo');
            }
        }else{
            return $this->error(lang('密码不一致'),$url = null,'',$wait=3);
        }
    }
    //i9ch3f2ca----密码数据库-----用户名root
    ////////////////////////////////登陆//////////////////////////////////
    /**
     * [getlogin 登陆界面]
     * @return [type] [description]
     */
    public function getlogin()
    {
        $b = $this->old();
        if (empty($b)) {
            $b['phone'] = '';
            $b['imgCode'] = '';
        }
        //return view('user/login'); 
        if (model('Users')->checkLogin()) {
            //会员首页
            $this->redirect('/user/index');
        }else{
            return view('user/login',['b'=>$b]);
        }   
    }
    /**
     * [getperson 登陆成功界面]
     * @return [type] [description]
     */
    public function getperson()
    {
        return view('user/myhomepage');
    }
    /**
     * [postloginsuccess 登陆界面表单]
     * @return [type] [description]
     */
    public function postloginsuccess()
    {
        $this->flash();
        $account = input('post.phone');
        $password = input('post.password');
        //halt(Session::pull('redirect_url'));
        //图片验证码 
        $code=input('post.imgCode');
        $captcha = new \think\captcha\Captcha(config('captcha'));
        if (!$captcha->check($code)) {
            return $this->error(lang('code is error'));
        }
        $re=model('Users')->login($account,$password,false);
        $user_id=model('Users')->getLoginUserInfo('id');
        if ($re===true) {
            $url = Session::pull('redirect_url') ? : '/user/index';
            model('Users')->where('id',$user_id)->setField('login_session_id',session('user.login_session_id'));
            return $this->redirect($url);
        }else{
            return $this->error(model('Users')->getError());
        }

    }
    /**
     * [getloginout 退出登录]
     * @return [type] [description]
     */
    public function getloginout()
    {
        if (model('Users')->logout()) {
            $this->redirect(url('/web/index','','','www'));
        }
    }
    /**
     * [getindex 会员首页]['s'=>['<=',$sdate],'e'=>['>=',$edate]]
     * @return [type] [description]
     */
    public function getindex()
    {
        $user_id = session('user.id');
        $userinfo = model('Users')->where('id',$user_id)->find();
        if ($userinfo->real_name && $userinfo->email) {
            $this->assign('userinfo',$userinfo);
            //$query = ['status','<>',-1];
            $list = model('Orders')->getOrders();//halt($list);
            $this->assign('list',$list);
            //待付款==>100
            $order_d_f = model('Orders')->where(['status'=>100,'rent_user_id'=>$user_id])->count();
            $this->assign('order_d_f',$order_d_f);
            //待入住==>101
            $order_d_r = model('Orders')->where(['status'=>101,'rent_user_id'=>$user_id])->count();
            $this->assign('order_d_r',$order_d_r);
            //我的需求
            $wish = model('Wishs')->getWishList();
            $wish_num = count($wish);
            $this->assign('wish_num',$wish_num);
            //我的抢单
            $wish_q_d = db('wish_users')->where('meet_user_id',$user_id)->count();
            $this->assign('wish_q_d',$wish_q_d);
            //我的消息
            $message_num = model('UserMessages')->where(['users_id'=>$user_id,'status'=>0])->count();
            $this->assign('message_num',$message_num);
            return view('user/myhomepage');
        }
        return $this->redirect('@userinfo/personinfo');
    }
    /////////////////////////////////////////
    /**
	 * ajax返回的数据
	 */
	public function returnAjax($num,$msg){
		return ['num'=>$num, 'msg'=>$msg];		
	}
}

